﻿using System.Collections.ObjectModel;
using YKXiangQi.Common;
using YKXiangQi.Models;
using YKXiangQi.Utilities;
using System.Linq;

namespace YKXiangQi.ViewModels
{
    public class SettingsViewModel: DialogViewModel
    {
        private IDialogService DialogService { get; set; }

        private ObservableCollection<Engine> _engines;
        public ObservableCollection<Engine> Engines
        {
            get { return _engines; }
            set
            {
                _engines = value;
                RaisePropertyChanged(() => Engines);
            }
        }

        public RelayCommand CancelCommand { get; set; }
        public RelayCommand OkCommand { get; set; }
        public RelayCommand DeleteEngineCommand { get; set; }
        public RelayCommand ApplyCommand { get; set; }
        public RelayCommand AddEngineCommand { get; set; }
        public RelayCommand EditEngineCommand { get; set; }

        public SettingsViewModel()
        {
            CancelCommand = new RelayCommand(OnCancel);
            OkCommand = new RelayCommand(OnOk);
            DeleteEngineCommand = new RelayCommand(DeleteEngine);
            ApplyCommand = new RelayCommand(OnApply);
            AddEngineCommand = new RelayCommand(AddEngine);
            EditEngineCommand = new RelayCommand(EditEngine);
            DialogService = new DialogService();
            LoadSettings();
        }

        private void OnCancel(object param)
        {
            OnRequestClose();
        }

        private void OnOk(object param)
        {
            OnApply(null);
            OnRequestClose();
        }

        private void LoadSettings()
        {
            Engines = new ObservableCollection<Engine>(Engine.LoadEnginesFromSettings().OrderBy(engine => engine.Name));
        }

        private void SaveSettings()
        {
            Properties.Settings.Default.Engines = Engines.Serialize().ToBase64();
            Properties.Settings.Default.Save();
        }

        private void DeleteEngine(object param)
        {
            var engine = param as Engine;
            if (engine == null) return;

            Engines.Remove(engine);
            UserChanged();
        }

        private void OnApply(object param)
        {
            SaveSettings();
            UserCommitted();
        }

        private void AddEngine(object param)
        {
            var engine = new Engine();
            bool? dialogResult = DialogService.ShowDialog("Add Engine", new EngineDetailsViewModel(engine), "settings.png");
            if(dialogResult.HasValue && dialogResult.Value)
            {
                _engines.Add(engine);
                _engines = new ObservableCollection<Engine>(_engines.OrderBy(x => x.Name));
                RaisePropertyChanged(() => this.Engines);
            }
        }

        private void EditEngine(object param)
        {
            var engine = param as Engine;
            if (engine == null) return;

            var clonedEngine = engine.DeepClone();
            bool? dialogResult = DialogService.ShowDialog("Edit Engine", new EngineDetailsViewModel(clonedEngine), "settings.png");
            if (dialogResult.HasValue && dialogResult.Value)
            {
                _engines.Remove(engine);
                _engines.Add(clonedEngine);
                _engines = new ObservableCollection<Engine>(_engines.OrderBy(x => x.Name));
                RaisePropertyChanged(() => this.Engines);
            }
        }
    }
}
